using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public class DonViDAO
    {
        DataProvider Provider;
        public DonViDAO()
        {
            Provider = new DataProvider();
        }

        public DataTable LayMaDonVi()
        {
            Provider.Connect();
            string str = "Select MaDV From DONVI Where MaDV >= all (Select MaDV From DONVI)";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }

        public DataTable LayBangDonVi()
        {
            Provider.Connect();
            string str = "select * from DONVI";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }


        public void ThemDonVi(DonViDTO dvDto)
        {
            Provider.Connect();
            string str;
            str = "Insert into DONVI(TenDV, MaDV) values (?, ?)";

            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@TenDV", OleDbType.WChar);
            cmd.Parameters.Add("@MaDV", OleDbType.WChar);

            cmd.Parameters["@TenDV"].Value = dvDto.TenDV;
            cmd.Parameters["@MaDV"].Value = dvDto.MaDV;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();

        }


        public void XoaDonVi(string maDV)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Delete From DonVi Where MaDV = ?";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@MaDV", OleDbType.Char);
            cmd.Parameters["@MaDV"].Value = maDV;
            cmd.ExecuteNonQuery();
            // B5: Dong ket noi CSDL
            Provider.Disconnect();
        }

        public void SuaDonVi(DonViDTO dvDto)
        {
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Update DonVi Set TenDV = ? Where MaDV = ? ";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@TenDV", OleDbType.WChar);
            cmd.Parameters.Add("@MaDV", OleDbType.WChar);

            cmd.Parameters["@TenDV"].Value = dvDto.TenDV;
            cmd.Parameters["@MaDV"].Value = dvDto.MaDV;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();
        }


        public DataTable TimKiemTheoMaDV(string maDV)
        {
            Provider.Connect();
            DataTable dt = new DataTable();
            //B3: Tao chuoi strSQL thao tac CSDL
            string str = "Select * From DonVi Where MaDV = " + "'" + maDV + "'";
            //B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@MaDV", OleDbType.WChar);
            cmd.Parameters["@MaDV"].Value = maDV;

            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }
    }
}
